package com.aurora.oa.message.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.message.adapter.ContactsAdapter;
import com.aurora.oa.message.bean.ContactsDataJsonBean;
import com.aurora.oa.message.bean.ContactsGroupBean;
import com.aurora.oa.message.bean.ContactsInfo;
import com.aurora.oa.message.presenter.ContactsListActivityPresenter;
import com.aurora.oa.message.presenter.ContactsListActivityPresenterImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/23 0023.
 */
public class ContactsListActivity extends BaseActivity implements ContactsListActivityView{

    RecyclerView rv_contacts_list;
    ContactsAdapter adapter;
    ContactsListActivityPresenter mPresenter;
    List<ContactsInfo> contactsInfoList = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_contacts_list);
        initTitlebar(true,"组织架构",true);
        mPresenter = new ContactsListActivityPresenterImpl(this);
        mPresenter.getContanctsListData();
        rv_contacts_list = (RecyclerView) findViewById(R.id.rv_contacts_list);
        rv_contacts_list.setLayoutManager(new LinearLayoutManager(this));
        rv_contacts_list.addItemDecoration(new ItemDivider().setDividerWith(1).setDividerColor(Color.TRANSPARENT));
        rv_contacts_list.setItemAnimator(new DefaultItemAnimator());

    }

    @Override
    public void showContanctsData(List<ContactsGroupBean> mContactsData) {
        adapter = new ContactsAdapter(this,mContactsData,false);
        rv_contacts_list.setAdapter(adapter);
    }



    @Override
    protected void setResult() {
        String ids="";
        if(adapter!=null){
            Map<String ,Object> contactsAndIDs=adapter.getSelectedContactsAndIDs();
            ids  = (String) contactsAndIDs.get("ids");
            contactsInfoList = (List<ContactsInfo>) contactsAndIDs.get("contactsList");
        }
        Intent intent = new Intent();
        intent.putExtra("who_to_send",ids);
        intent.putExtra("contacts_list", (Serializable) contactsInfoList);
        setResult(RESULT_OK,intent);
    }

    @Override
    public void onBackPressed() {
        String ids="";
        if(adapter!=null){
            ids  =adapter.getCheckedUserIDs();
        }
        Intent intent = new Intent();
        intent.putExtra("who_to_send",ids);
        setResult(RESULT_OK,intent);
        finish();
    }
}
